package com.cq;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class BaseUtil {
	/*
	 * 判断方法是否在对象中
	 */
	public static Method getMethodFunction(String methodName,Object object){
		if(methodName == null || object==null){
			return null;
		}
		Method[] methods = object.getClass().getMethods();
		for(Method method : methods){
			if(methodName.equals(method.getName())){
				return method;
			}
		}
		return null;
	}

	/*
	 * 获取get方法名
	 */
	public static String getGetMethodName(String paramName){
		StringBuffer sb = new StringBuffer();
		return sb.append("get").append(paramName.substring(0,1).toUpperCase())
				.append(paramName.substring(1)).toString();
	}
	
	/*
	 * 注入单一参数值
	 */
	public static void setParam(Object dbBean,String setParamName,Object setParamValue) throws Exception {
		String setMethodName = getSetMethodName(setParamName);
		getMethodFunction(setMethodName,dbBean).invoke(dbBean, setParamValue);
	}
	
	/*
	 * 获取单一参数值
	 */
	public static Object getParam(Object bean,String getParamName) throws Exception {
		String getMethodName = BaseUtil.getGetMethodName(getParamName);
		return BaseUtil.getMethodFunction(getMethodName,bean).invoke(bean);
	}
	
	/*
	 * 获取set方法名
	 */
	public static String getSetMethodName(String paramName){
		StringBuffer sb = new StringBuffer();
		return sb.append("set").append(paramName.substring(0,1).toUpperCase())
				.append(paramName.substring(1)).toString();
	}
	

	/*
	 * 获取参数类型
	 */
	public static Object getParamType(String paramName,Object dbBean){
		Object type = null;
		Field[] fields = dbBean.getClass().getFields();
		for(Field field : fields){
			if(field.getName().equals(paramName)){
				type = field.getType();
				break;
			}
		}
		return type;
	}
	
	/*
	 * 获取参数
	 */
	public static Object getParamValue(String setParamValue,Object type){
		if(type.equals(Integer.class)){
			return Integer.valueOf(setParamValue);
		}else if(type.equals(Byte.class)){
			return Byte.valueOf(setParamValue);
		}else if(type.equals(Long.class)){
			return Long.valueOf(setParamValue);
		}else if(type.equals(Date.class)){
			return UtilDate.getDateFromStr(setParamValue);
		}else{
			return setParamValue;
		}
	}
	
	/*
	 * excel下载
	 */
	public static void downloadExcel(HttpServletResponse response,HSSFWorkbook workbook){
		if(workbook !=null){  
			 try{  
				 String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";  
                String headStr = "attachment; filename=\"" + fileName + "\"";  
                response.setContentType("APPLICATION/OCTET-STREAM");  
                response.setHeader("Content-Disposition", headStr);  
                OutputStream out = response.getOutputStream();
                workbook.write(out);
                out.close();
            }catch (IOException e){  
                e.printStackTrace();  
            }  
       }  
	}
}
